<?php
namespace Home\Controller\User;
use Think\Controller;

class IndexController extends Controller
{
    /*
     * function index
     * 用户登录
     * return bool
     */
    public function index()
    {
        if(cookie('username') == null || cookie('pwd') == null){
            return redirect('/drm/');
        }

        $page = max(1,trim(I('GET.page')));
        $limit = trim(I('GET.limit')) ? : (int) 20;
        $start = ($page-1)*$limit;

        $users = M('user');
        $data = $users->where()->page($start,$limit)->select();
        $total = $users->where()->count();

        $user['data'] = $data;
        $user['meta']['total'] = $total;

        $person['name'] = cookie('username');
        $this->assign('person',$person);
        $this->assign('data',$user);
        $this->display('User/list');
    }

    /*
     * 添加新用户
     * @params POST
     * return bool
     */
    public function userAdd()
    {
        if(IS_POST){
            $name = trim(I('POST.username'));
            $pwd = trim(I('POST.password'));
            $user['username'] = $name;
            $user['pwd'] = md5($name);
            $user['ctime'] = time();

            $users = M('user');
            $res = $users->data($user)->add();
            if($res){
                return redirect('/drm/');
            }else{
                return $this->error('创建用户失败,请重试');
            }
        }
        $person['name'] = cookie('username');
        $this->assign('person',$person);
        $this->assign('type','userAdd');
        $this->display('User/add');
    }

    /*
     * function userDelete
     * 删除用户
     * @params id
     * return bool
     */
    public function userDelete()
    {
        if(cookie('username') == null || cookie('pwd') == null){
            return redirect('/drm/');
        }

        if(!$id = trim(I('POST.id'))){
            return null;
        }
        $users = M('user');
        return $users->where("id = $id")->delete();
    }

    /**
     * userEdit 
     * 
     * todo 取消此功能
     * @access public
     * @return void
     */
    public function userEdit()
    {
        //if(cookie('username') == null || cookie('pwd') == null){
            //return redirect('/drm/');
        //}

        //if(!$id = trim(I('id'))){
            //return false;
        //}
        //$users = M('user');
        //if(IS_POST){
            //I('POST.username') && $data['username'] = trim(I('POST.username'));
            //I('POST.password') && $data['pwd'] = md5(trim(I('POST.username')));

            //$res = $users->where("id = $id")->save($data);
            //if($res){
                //return $this->redirect('User/userShow',array(),2,'页面跳转中');
            //}else{
                //return $this->error('没有修改哦,请重试');
            //}
        //}
        //$user = $users->where("id = $id")->find();
        //$person['name'] = cookie('username');
        //$this->assign('person',$person);
        //$this->assign('type','userEdit');
        //$this->display('User/add');
    }

    /**
     * userCheck 
     * 
     * 检查用户名是否存在
     * @access public
     * @return void
     */
    public function userCheck()
    {
        if(!$name = trim(I('POST.name'))){
            $this->ajaxReturn((int) 2);
        }

        $users = M('user');
        $user = $users->where("username = '$name'")->find();
        $res = $user['username'] ? (int) 1 : (int) 2;
        $this->ajaxReturn($res);
    }

    /**
     * userRecover 
     * 
     * 用户找回密码
     * @access public
     * @return void
     */
    public function userRecover()
    {
        if(IS_POST){
            if(!$username = I('username')){
                return $this->error('不能缺失用户名');
            }
            if(!$pwd = I('pwd')){
                return $this->error('密码不能为空');
            }
            if(I('pwd') != I('rePwd')){
                return $this->error('两次密码要一致哦');
            }
            $data['pwd'] = md5(trim($pwd));

            $users = M('user');
            $res = $users->where("username = '$username'")->save($data);
            if($res){
                return redirect('/drm/');
            }else{
                return $this->error('不能和原密码一样哦');
            }
        }
        $this->display('User/recover');
    }

    /**
     * userLogout 
     * 
     * 用户登出操作
     * @access public
     * @return void
     */
    public function userLogout()
    {
        cookie('username',null);
        cookie('pwd',null);
        cookie('class',null);
        return redirect('/drm/');
    }
}
